#include "StopWatch.h"

StopWatch::StopWatch(void) : _measuring(false), _timeValid(false)
{
	memset(&_freq, 0, sizeof(_freq));
	memset(&_start, 0, sizeof(_start));
	memset(&_end, 0, sizeof(_end));

	QueryPerformanceFrequency(&_freq);
}


StopWatch::~StopWatch(void)
{
}

void StopWatch::Start() {
  if (_measuring) { return; } 

	QueryPerformanceCounter(&_start);
  _timeValid = true;
  _measuring = true;
}
void StopWatch::Stop() {
  if (!_measuring) { return; }

	QueryPerformanceCounter(&_end);
  _timeValid = true;
  _measuring = false;
}
void StopWatch::Reset() {
  _measuring = _timeValid = false;
}

LONGLONG StopWatch::Ellapsed() {
	if (_measuring) {
		LARGE_INTEGER now;
		QueryPerformanceCounter(&now);
		return (now.QuadPart - _start.QuadPart) * 1000 / _freq.QuadPart;
	}
	else {
		return (_timeValid) ? (_end.QuadPart - _start.QuadPart) * 1000 / _freq.QuadPart
												: -1;
	}
}
bool StopWatch::TimeValid() {
  return _timeValid;
}